<template>
  <div class="mod-role-table">
    <div class="mod-role-table-tree">
      <el-form size="small" :model="queryForm">
        <el-form-item>
          <el-input v-model="queryForm.roleName" placeholder="角色名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-tree
            :data="roleTreeList"
            :props="roleTreeListTreeProps"
            node-key="id"
            ref="roleTree"
            @node-click="nodeClick"
            :highlight-current="true"
            :expand-on-click-node="false"
            :default-expand-all="true"
            :filter-node-method="searchRoleNode"
          ></el-tree>
        </el-form-item>
      </el-form>
    </div>
    <div class="mod-role-table-table">
      <el-form size="small" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.userName" placeholder="用户名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="dataList"
        size="mini"
        row-key="id"
        lazy
        border
        :load="load"
        v-loading="dataListLoading"
        style="width: 100%"
      >
        <el-table-column type="index" align="center" label="编号" width="100" />
        <el-table-column
          prop="name"
          header-align="center"
          min-width="150"
          align="left"
          label="中文名称"
        ></el-table-column>

        <el-table-column
          prop="code"
          header-align="center"
          min-width="100"
          align="center"
          label="编码"
        ></el-table-column>

        <el-table-column
          prop="physicalName"
          header-align="center"
          min-width="150"
          align="center"
          label="物理名称"
        ></el-table-column>

        <el-table-column prop="query" header-align="center" align="center" label="查询权限">
          <template slot-scope="scope">
            <el-switch
              active-text="开启"
              inactive-text="关闭"
              v-model="scope.row.query"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="addRole(scope.row,'query')"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="edit" header-align="center" align="center" label="编辑权限">
          <template slot-scope="scope">
            <el-switch
              active-text="开启"
              inactive-text="关闭"
              v-model="scope.row.edit"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="addRole(scope.row,'edit')"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="delete" header-align="center" align="center" label="删除权限">
          <template slot-scope="scope">
            <el-switch
              active-text="开启"
              inactive-text="关闭"
              v-model="scope.row.delete"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="addRole(scope.row,'delete')"
            ></el-switch>
          </template>
        </el-table-column>

        <el-table-column prop="add" header-align="center" align="center" label="添加权限">
          <template slot-scope="scope">
            <el-switch
              active-text="开启"
              inactive-text="关闭"
              v-model="scope.row.add"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="addRole(scope.row,'add')"
            ></el-switch>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { roleList as pageRoleTreeList } from "@/api/role";

import { pageList, addRoleDb, addRoleTable, addRoleField } from "@/api/table";
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      queryForm: {
        unitName: ""
      },
      dataForm: {
        roleName: "",
        roleId: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      roleTreeList: [],
      roleTreeListTreeProps: {
        label: "name",
        children: "children"
      }
    };
  },
  watch: {
    "queryForm.roleName": function(val) {
      this.$refs.roleTree.filter(val);
    }
  },
  components: {},
  activated() {
    this.getDataList();
    this.getRoleList();
  },
  methods: {
    searchRoleNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    nodeClick(data, node) {
      console.log(data);
      this.dataForm.roleId = data.id;
      this.getDataList();
    },
    // 获取角色数据列表
    getRoleList() {
      pageRoleTreeList({})
        .then(res => {
          if (res && res.code === 0) {
            this.roleTreeList = treeDataTranslate(res.data, "id");
          }
        })
        .then(res => {
          if (this.roleTreeList.length) {
            this.dataForm.roleId = this.roleTreeList[0].id;
            this.roleTableListTreeSetCurrentNode();
          }
        })
        .then(res => {
          this.getDataList();
        });
    },
    load(tree, treeNode, resolve) {
      this.loadTablelist(
        {
          type: tree.type,
          parentId: tree.id,
          queryPermissions: 1
        },
        resolve
      );
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      pageList({ type: 1, queryPermissions: 1 }).then(res => {
        if (res && res.code === 0) {
          this.dataList = treeDataTranslate(res.data, "id");
        }
        this.dataListLoading = false;
      });
    },
    loadTablelist(data, resolve) {
      this.dataListLoading = true;
      pageList(data).then(res => {
        if (res && res.code === 0) {
          resolve(treeDataTranslate(res.data, "id"));
        }
        this.dataListLoading = false;
      });
    },
    // 菜单树设置当前选中节点
    roleTableListTreeSetCurrentNode() {
      this.$refs.roleTree.setCurrentKey(this.dataForm.unitId);
      this.dataForm.parentName = (this.$refs.roleTree.getCurrentNode() || {})[
        "name"
      ];
    },
    addRole(row, type) {
      debugger
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let addRoleEvent = addRoleDb;
          if (row.type == 2) {
          } else if (row.type == 3) {
            enableEvent = addRoleTable;
          } else if (row.type == 4) {
            enableEvent = addRoleField;
          }
          addRoleEvent({ id: row.id,state:row[type],type:type }).then(res => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                }
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    }
  }
};
</script>

<style lang="scss">
.mod-role-table {
  position: relative;
}

.mod-role-table .mod-role-table-tree {
  position: relative;
  width: 230px;
  min-height: 100%;
  height: 100%;
  float: left;
}
.mod-role-table .mod-role-table-table {
  position: relative;
  margin-left: 235px;
  min-height: 100%;
  top: 0px;
}
</style>